package com.lagou.edu.controller;

import com.lagou.edu.service.ICodeServer;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description TODO
 * @Classname CodeController
 * @Date 2021/5/30 17:17
 * @Author ch'ien
 */
@RestController
@RequestMapping("/code")
public class CodeController {

    @Autowired
    private ICodeServer codeServer;


    @GetMapping("/create/{email}")
    public Boolean create(@PathVariable String email){

        return codeServer.create(email);
    }

    @HystrixCommand(
            // 线程池标识，要保持唯一，不唯一的话就共用了
            threadPoolKey = "validateCodeTimeout",
            // 线程池细节属性配置
            threadPoolProperties = {
                    @HystrixProperty(name="coreSize",value = "1"), // 线程数
                    @HystrixProperty(name="maxQueueSize",value="20") // 等待队列长度
            },
            fallbackMethod = "validateFallback",
            commandProperties = {
                    // 每一个属性都是一个HystrixProperty
                    @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value="2000")
            }
    )
    @GetMapping("/validate/{email}/{code}")
    public int validate(@PathVariable String email, @PathVariable String code){

        return codeServer.validate(email, code);
    }

    public int validateFallback(@PathVariable String email, @PathVariable String code){

        return 2;
    }
}
